package com.sxbbc.agent.util;

import com.sxbbc.common.core.dto.jz.MenusVo;
import com.sxbbc.common.core.entity.jz.AgAgent;
import com.sxbbc.common.core.entity.jz.AgMenus;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 代理 session工具
 * @author leehao
 */
public class AgentSession {

    /**
     * 验证码
     */
    public static final String VALID_CODE = "session_validCode";

    /**
     * 用户id
     */
    public static final String AGENT_ID = "session_agentId";

    /**
     * 菜单
     */
    public static final String MENU_LIST = "session_menusList";

    /**
     * 菜单URL
     */
    public static final String MENU_URL = "session_menuUrl";

    /**
     * 菜单
     */
    public static final String MENU = "session_menu";

    public static final String AGENT_USER = "session_agent";

    /**
     * 退出
     */
    public static void exit(HttpServletRequest request) {
        request.getSession().removeAttribute(MENU_LIST);
        request.getSession().removeAttribute(AGENT_ID);
    }

    /**
     * 菜单设置session
     */
    public static void setMenusList(HttpServletRequest request, List<MenusVo> list) {
        request.getSession().removeAttribute(MENU_LIST);
        request.getSession().setAttribute(MENU_LIST, list);
    }

    /**
     * 菜单获取session
     */
    public static List<MenusVo> getMenusList(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<MenusVo> list = (List<MenusVo>) request.getSession().getAttribute(MENU_LIST);
        return list;
    }

    /**
     * 菜单URL获取session
     */
    public static List<String> getMenuUrl(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<String> list = (List<String>) request.getSession().getAttribute(MENU_URL);
        return list;
    }

    /**
     * 菜单URL设置session
     */
    public static void setMenuUrl(HttpServletRequest request, List<String> list) {
        request.getSession().removeAttribute(MENU_URL);
        request.getSession().setAttribute(MENU_URL, list);
    }

    /**
     * 菜单获取session
     */
    public static List<AgMenus> getMenu(HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<AgMenus> list = (List<AgMenus>) request.getSession().getAttribute(MENU);
        return list;
    }

    /**
     * 菜单设置session
     */
    public static void setMenu(HttpServletRequest request, List<AgMenus> list) {
        request.getSession().removeAttribute(MENU);
        request.getSession().setAttribute(MENU, list);
    }

    /**
     * 验证码对象设置session
     */
    public static void setValidCode(HttpServletRequest request, String validCode) {
        request.getSession().removeAttribute(VALID_CODE);
        request.getSession().setAttribute(VALID_CODE, validCode);
    }

    /**
     * 验证码对象获取session
     */
    public static String getValidCode(HttpServletRequest request) {
        String validCode = (String) request.getSession().getAttribute(VALID_CODE);
        return validCode;
    }

    public static void setAgent(HttpServletRequest request, AgAgent agent){
        request.getSession().removeAttribute(AGENT_USER);
        request.getSession().setAttribute(AGENT_USER,agent);
    }

    public static AgAgent getAgent(HttpServletRequest request){
        AgAgent agent = (AgAgent) request.getSession().getAttribute(AGENT_USER);
        return agent;
    }
}
